<?php

namespace app\api\controller;

use app\adminapi\logic\RepairLogic;
use app\api\validate\InsurancePolicyValidate;
use app\api\validate\RepairValidate;
use app\common\model\Huanxin;
use app\common\model\InsurancePolicy;
use app\common\model\Repair;
use app\common\model\user\User;

class RepairController extends BaseApiController
{
    /**
     * @notes 新增
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        $params = (new RepairValidate())->post()->goCheck('add');
        $params['user_id'] = $this->userId;
        $user = User::findOrFail($this->userId);

        $insurancePolicy = InsurancePolicy::where('id', $params['insurance_policy_id'])->findOrFail();

        $repair = RepairLogic::addUserRepair($user, $insurancePolicy, $params['imgs'], $params['remark']);

        // 查找换新价格
        /**
         * @var Huanxin $huanxinConfig
         */
        $huanxinConfig = Huanxin::where('spec', $insurancePolicy->spec)->where('disk', $insurancePolicy->disk)->find();
        if (!$huanxinConfig) {
            return $this->fail('未找到对应的免赔价格');
        }
        if ($insurancePolicy->product_type_id == 1) {
            $repair->order_amount = $huanxinConfig->suiping_money;
        } elseif ($insurancePolicy->product_type_id == 2) {
            $repair->order_amount = $huanxinConfig->yanbao_money;
        } elseif ($insurancePolicy->product_type_id == 3) {
            $repair->order_amount = $huanxinConfig->dianchi_money;
        } elseif ($insurancePolicy->product_type_id == 4) {
            $repair->order_amount = $huanxinConfig->huanxin_money;
        }
        $repair->save();
        return $this->success('ok', [
            'order_id'     => (int)$repair->id,
            'order_amount' => $repair->order_amount,
            'from'         => 'huanxin'
        ]);
    }


    public function info()
    {
        $repair = Repair::with(['insurance.orderinfo'])->findOrFail($this->request->param('id'));
        $arr = $repair->toArray();
        $arr['store_info'] = [
            'store_name'    => '',
            'store_phone'   => '',
            'store_address' => '',
        ];
        return $this->success('ok', $arr);
    }


    /**
     * @notes 填写快递信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function fillInExpress()
    {
        $params = (new RepairValidate())->post()->goCheck('fillIn');
        $repair = Repair::findOrFail($params['repair_id']);
        RepairLogic::fillInExpress($repair, $params['express_no'], $params['express_type'], $params['receive_address'],
            $params['receive_concate_name'], $params['receive_concate_phone']);
        return $this->success('ok', $repair->toArray());
    }


    /**
     * @notes 搜索 对应的保单
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function searchInsurancePolicy()
    {
        $id = $this->request->param('id');

        $insurancePolicy = \app\common\model\InsurancePolicy::where('id', $id)->with(['orderinfo'])->scope('effective')->find();
        if (!$insurancePolicy) {
            return $this->fail('未找到该对应的保单');
        }
        $arr = $insurancePolicy->toArray();
        $arr['store_info'] = [
            'store_name'    => '',
            'store_phone'   => '',
            'store_address' => '',


        ];
        return $this->success('ok', $arr);
    }

    /**
     * @notes 列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function items()
    {
        /**
         * @var User $user
         */
        $user = User::findOrEmpty($this->userId);

        $query = $user->repairList()->with(['insurance']);
        $status = $this->request->param('status');
        if ($status != -1) {
            $query->where('status', $status);
        }


        $items = $query->paginate(10);
        return $this->success('ok', $items->toArray());
    }

    public function expressList()
    {
        $list = [
            [
                'delivery_name' => '京东',
                'delivery_id'   => 1
            ],
            [
                'delivery_name' => '顺丰',
                'delivery_id'   => 2
            ]
        ];


        return $this->success('ok', $list);
    }


    public function huanxin()
    {
        $params = (new RepairValidate())->post()->goCheck('huanxin');
        $params['user_id'] = $this->userId;
        $user = User::findOrFail($this->userId);

        $insurancePolicy = InsurancePolicy::where('id', $params['insurance_policy_id'])->findOrFail();
        if ($insurancePolicy->product_type_id != 4) {
            return $this->fail('该保单不是换新保单');
        }
        if ($insurancePolicy->status != 20) {
            return $this->fail('该保单未生效');
        }

        $repair = RepairLogic::addUserRepair($user, $insurancePolicy);

        // 查找换新价格
        /**
         * @var Huanxin $huanxinConfig
         */
        $huanxinConfig = Huanxin::where('spec', $insurancePolicy->spec)->where('disk', $insurancePolicy->disk)->find();
        if (!$huanxinConfig) {
            return $this->fail('未找到对应的换新价格');
        }
        $repair->order_amount = $huanxinConfig->huanxin_money;

        $repair->save();
        return $this->success('ok', [
            'order_id' => (int)$repair->id,
            'from'     => 'huanxin'
        ]);
    }


}